function ViewSDK(doc_id,docUrl,pageUrl,dataUrl) {
	this.m_lsPageView = [];
	this.m_docId = doc_id;
	this.m_docUrl = docUrl;
	this.m_pageUrl = pageUrl;
    this.pdDoc = null;
	this.callback = null;
	this.beforeload = 0;
	this.afterload = 0;
    var _self = this,
		_fontLoader = null,
		_pageIndex = 0,
		_type = 0,
		m_annotList = [],
		_CompresseType = ".zip",
		_dataUrl = dataUrl.charAt(0) == "/"?dataUrl.substr(1,dataUrl.length):dataUrl,
		_drawPageStatus = 0,
		_pageCanvas = null;

    this.OpenPDDocument = function (callback) {
        //1.download docBin data;
        //2.parse docBin and create PDDoc object;
        //3.create EditorDoc object by PDDoc object;
    	this.beforeload = (new Date()).getTime(); 
		this.callback = callback;
		_type = 1;
		DownloadData(CreatePDFDoc);
//		var dataMgr = new DataMgr();//this.m_docId,_pageIndex,"GetAnnotBin.do"
//		dataMgr.url = _dataUrl + "doc" + _CompresseType;
//		dataMgr.DownData(CreatePDFDoc);
    };
	function CreatePDFDoc(buf){
    	var rd = new BinaryReader(buf);
    	_self.pdDoc = new PDDoc();
    	_self.pdDoc.ParseFromBin(rd);
    	_self.pdDoc.docId = _self.m_docId;
		if(_dataUrl.charAt(_dataUrl.length -1)!="/")_dataUrl = _dataUrl + "/";
		_self.pdDoc.m_fontPath = _dataUrl + "font/";
		_self.pdDoc.m_imgPath = _dataUrl + "img/";
		
		_fontLoader = new FontLoader(_self.pdDoc);
		_fontLoader.standFont = "Editor1.0/standFont";
		_fontLoader.webFont = "Editor1.0/webfont.js";
		if(_self.callback != null){
			_self.pdDoc != null?_self.callback("success"):_self.callback("failed");
		}
    };
   
    function getAnnotBuf(buf){
    	var pdPage = _self.GetPDFPage(_pageIndex);
    	var rd = new BinaryReader(buf);
    	pdPage.LoadAnnotObj(rd);
    	var pageView = this.m_lsPageView[_pageIndex];
    	pageView.m_annotList = pdPage.m_annotList;
    	pageView.ShowAnnots();
    };
	function DownloadData(callback){
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function () {
			if (xhr.readyState == xhr.DONE) {
				var status = "failed";
				if (xhr.response) {
						if(xhr.status == 200){
							console.log(xhr.response);
							readFileToArrayBuffer(xhr.response,callback,_self);
							status = "success";
						}					
					}
				if(status == "failed"){
					_drawPageStatus = 2;
					if(_self.callback != null)_self.callback(status);
				}
			}
		};
		var url = _type == 1?_self.m_docUrl + "?fileid=" + _self.m_docId:_self.m_pageUrl + "?fileid=" + _self.m_docId + "&page=" + _pageIndex;
		xhr.open("POST", url, true);
		xhr.responseType = "blob";
		xhr.send(); 
	};

    //return page count;
    this.GetPageCount = function () {
		return this.pdDoc.GetCountPages();
    };

    //get page size in pdf unit, return a array such as [width, height]
    this.GetPageSize = function (index) { 
		return this.pdDoc.GetPageSize(index);
	};

	this.GetPDFPage = function (pageIndex){
		return this.pdDoc.GetPageByIndex(pageIndex);
	};
    
	this.GetDrawPageStatus = function(){
		return _drawPageStatus;
	};
	this.SetDrawPageStatus = function(drawPageStatus){
		_drawPageStatus = drawPageStatus;
	};
	
	this.GetPageCanvas = function(){
		return _pageCanvas;
	};
	
    this.RenderPage = function (index, domPageDiv, start_x, start_y, size_x, size_y, rotate) {
        //1.create EditorPage object and EditorPageView object;
        //2.create canvas whitch with the EditorPageView object, and append it to domPageDiv;
        //3.download page bin data and parse it to PDPage object;
        //4.attach PDPage object to EditorPage object;
        //5.calculate scale factor;
        //6.set attributes with EditorPageView object for display;
        //7.call EditorPageView's Display function to render;
		
		var pageView = this.m_lsPageView[index];
		if(!pageView){
			pageView = new EditorPageView();
			pageView.m_nPageIndex = index;
			pageView.m_pdDoc = _self.pdDoc;
			pageView.CreateCanvas(domPageDiv);
			this.m_lsPageView[index] = pageView;
		}			
			pageView.m_width = size_x-start_x;
			pageView.m_height = size_y-start_y;			
			_pageIndex = index;
			_drawPageStatus = 1;
			if(null == pageView.GetPDPage()){
				_fontLoader.callback = function(){
					DrawPage(pageView,start_x, start_y,rotate);
				};
				_fontLoader.caller = _self;
				_type = 0;
				DownloadData(ParsePage);
//				var dataMgr = new DataMgr();
//				dataMgr.url = encodeURI(_dataUrl + _pageIndex + _CompresseType);
//				dataMgr.DownData(ParsePage);
			}else{
				DrawPage(pageView,start_x, start_y,rotate);
			}
    };

	function DrawPage(pageView,start_x, start_y,rotate){
		_pageCanvas = pageView.Display(start_x, start_y,rotate);
//		var dataMgr = new DataMgr(_self.m_docId,_pageIndex,"GetAnnotBin.do");
//		dataMgr.DownloadAnnotData(getAnnotBuf,_self);
		_self.afterload =  (new Date()).getTime();
		console.log("You Page Load took " + (_self.afterload-_self.beforeload) + " ms"); 
		_drawPageStatus = 3;
		_inProgress = 0;
	};

	function ParsePage(buf){
		if(!_self.pdDoc)return ;
		var rd = null;
		if(buf != null)rd = new BinaryReader(buf);
		_self.pdDoc.CreatePageFormBin(_pageIndex,rd);

        
		var pdPage = _self.GetPDFPage(_pageIndex);
		_self.m_lsPageView[_pageIndex].SetPDPage(pdPage);
        if(pdPage == null){
        	_drawPageStatus = 2;
        	return;
        }
		
		if ( pdPage.pdPageObjects.bFinishLoadImgs || pdPage.pdPageObjects.imgObjCount === 0 ) {
			_fontLoader.pageIndex = _pageIndex;
			_fontLoader.LoadFamiliesByPage.call(_fontLoader);
		}
		else {
			pdPage.pdPageObjects.callback = _fontLoader.LoadFamiliesByPage;
			pdPage.pdPageObjects.caller = _fontLoader;	
		}
		
	};

}

